package cn.trip.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.ibatis.io.ResolverUtil.IsA;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.trip.pojo.Trip_comment;
import cn.trip.service.comment.CommentService;

@Controller
@RequestMapping("test")
public class TestController {

	private Logger logger = Logger.getLogger(TestController.class);
	
	@Resource
	private CommentService commentService;
	
	@RequestMapping(value = "/addUser.do", method = RequestMethod.POST)
	//单文件上传
	public String addUserSave(Trip_comment comment,HttpSession session,HttpServletRequest request,
							 @RequestParam(value ="attach", required = false) MultipartFile attach){
		String idPicPath = null;
		//判断文件是否为空
		if(!attach.isEmpty()){
			String path = "D:/pic"; 
			logger.info("uploadFile path ============== > "+path);
			String oldFileName = attach.getOriginalFilename();//原文件名
			logger.info("uploadFile oldFileName ============== > "+oldFileName);
			String prefix=FilenameUtils.getExtension(oldFileName);//原文件后缀     
	        logger.debug("uploadFile prefix============> " + prefix);
			int filesize = 500000;
			logger.debug("uploadFile size============> " + attach.getSize());
	        if(attach.getSize() >  filesize){//上传大小不得超过 500k
            	request.setAttribute("uploadFileError", " * 上传大小不得超过 500k");
	        	return "useradd";
            }else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") 
            		|| prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式不正确
            	String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg";  
                logger.debug("new fileName======== " + attach.getName());
                File targetFile = new File(path, fileName);  
                if(!targetFile.exists()){  
                    targetFile.mkdirs();  
                }  
                //保存  
                try {  
                	attach.transferTo(targetFile);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "useradd";
                }  
                idPicPath = fileName;
            }else{
            	request.setAttribute("uploadFileError", " * 上传图片格式不正确");
            	return "useradd";
            }
		}
		comment.setCom_PicPath(idPicPath);
		if(commentService.addPic(comment)>0){
			return "comment/success";
		}
		return "useradd";
	}
	
	
	@RequestMapping(value="show")
	public String show(HttpServletRequest request,@RequestParam("com_id")String comid,Model model){
		Integer com_id = 0;
		if(comid != null){
			com_id = Integer.parseInt(comid);
		}
		Trip_comment comment = commentService.getComment(com_id);
		model.addAttribute("comment",comment);
		return "comment/show";
		
	}
	
}
